package com.example.wanghao.wangyixinwen.menu;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.GridView;
import android.widget.ListView;

import com.example.wanghao.wangyixinwen.R;
import com.example.wanghao.wangyixinwen.adapter.PicAdapter;
import com.example.wanghao.wangyixinwen.bean.PicBean;
import com.example.wanghao.wangyixinwen.home.BasePage;
import com.example.wanghao.wangyixinwen.utils.GsonTools;
import com.example.wanghao.wangyixinwen.utils.HMAPI;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.squareup.okhttp.Callback;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


/**
 * Created by wanghao on 2016/5/20.
 */
public class PicPage extends BasePage {

    private PicAdapter mPicAdapter;

    public PicPage(Context context) {
        super(context);
    }

    @ViewInject(R.id.lv_photo_list)
    private ListView lv_photo_list;
    @ViewInject(R.id.gv_photo_grid)
    private GridView gv_photo_grid;
    //用来记录组图界面是显示列表还是表格,false为列表型,显示listview,ture为表格型,显示gridview
    public boolean type = false;

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            mPicAdapter = new PicAdapter(newsLists, mContext);
            lv_photo_list.setAdapter(mPicAdapter);
        }
    };

    @Override
    public View initView() {
        /*TextView tv = new TextView(mContext);
        tv.setText("组图");*/
        View view = View.inflate(mContext, R.layout.photo, null);
        ViewUtils.inject(this, view);
        return view;
    }

    @Override
    public void initData() {
        getPicData();
    }

    private void getPicData() {
        final Request request = new Request.Builder().url(HMAPI.PHOTO_URL).build();


        OkHttpClient okhttpclient = new OkHttpClient();
        okhttpclient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Request request, IOException e) {

            }

            @Override
            public void onResponse(Response response) throws IOException {
                String result = response.body().string();
                System.out.println(result);
                parseJson(result);
            }
        });
    }

    private List<PicBean.DataBean.NewsBean> newsLists = new ArrayList<>();

    private void parseJson(String result) {
        PicBean picBean = GsonTools.changeGsonToBean(result, PicBean.class);
        System.out.println();
        newsLists.clear();
        newsLists.addAll(picBean.data.news);

        mHandler.sendEmptyMessage(0);
    }

    //根据当前的type类型设置显示listview还是gridview
    public void swithPageByType() {
        //用来记录组图界面是显示列表还是表格,
        if (type) {//ture为表格型,显示gridview
            lv_photo_list.setVisibility(View.GONE);
            gv_photo_grid.setVisibility(View.VISIBLE);
            if(gv_photo_grid.getAdapter() == null){
                gv_photo_grid.setAdapter(mPicAdapter);
            }
        } else {//false为列表型,显示listview,
            lv_photo_list.setVisibility(View.VISIBLE);
            gv_photo_grid.setVisibility(View.GONE);
            if(gv_photo_grid.getAdapter() == null){
                gv_photo_grid.setAdapter(mPicAdapter);
            }
        }
    }
}
